package com.sjsu.cloudmanager.client.ui;

import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;

/**
 * Created by IntelliJ IDEA.
 * User: ckempaiah
 * Date: 10/23/11
 * Time: 2:00 AM
 * To change this template use File | Settings | File Templates.
 */
public class RadioGroupBinding extends FieldBinding {

    public RadioGroupBinding(Field field, String property) {
        super(field, property);
    }

    @Override
    protected Object onConvertFieldValue(Object value) {
        if (((Radio)value).getValueAttribute().equalsIgnoreCase("inactive")){
            return new Boolean(false);
        } else {
            return new Boolean(true);
        }

    }

    @Override
    protected Object onConvertModelValue(Object value) {


        for (Field radio : ((RadioGroup)this.field).getAll()){

            if (Boolean.TRUE.equals(value) && ((Radio)radio).getValueAttribute().equalsIgnoreCase("active")){
                ((Radio)radio).setValue(Boolean.TRUE);
                return radio;
            } else if (Boolean.FALSE.equals(value) && ((Radio)radio).getValueAttribute().equalsIgnoreCase("inactive")){
                ((Radio)radio).setValue(Boolean.TRUE);
                return radio;
            }
        }
        return null;
    }
}
